Imports CapaDeDatos
Public Class nRecibo
    Private Recibo As dRecibo = New dRecibo()
    Private Retencion As nRetencion = New nRetencion()
    Private impRecCaja As nImpRecCaja = New nImpRecCaja()
    Private impRecRet As nImpRecRet = New nImpRecRet()
    Private impRecCuenta As nImpRecCuenta = New nImpRecCuenta()
    Private impRecCheque As nImpRecCheque = New nImpRecCheque()
    Private Cliente As CapaDeNegocios.nCliente = New CapaDeNegocios.nCliente
    Public Function Existe(ByVal Numero As String) As Boolean
        Return Recibo.Existe(Numero)
    End Function
    Public Function Insertar(ByVal Numero As String, ByVal Total As String, ByVal CodigoCliente As String, ByVal DtsImputaciones As DataSet) As DataSet

        Return Recibo.Insertar(Numero, Total, CodigoCliente)
    End Function
    Public Function Eliminar(ByVal idRecibo As String) As Boolean
        Return Recibo.Eliminar(idRecibo)
    End Function
    Public Function BuscarFiltrado(ByVal idRecibo As String, ByVal Numero As String, ByVal Fecha As String, ByVal Total As String, ByVal CodigoCliente As String) As DataSet
        Dim dts As DataSet
        dts = Recibo.BuscarFiltrado(idRecibo, Numero, Fecha, Total, CodigoCliente)
        dts.Tables(0).Columns.Add("RazonSocial")
        Dim i As Integer
        For i = 0 To dts.Tables(0).Rows.Count - 1
            Try
                dts.Tables(0).Rows(i)("RazonSocial") = Cliente.Listar(dts.Tables(0).Rows(i)("CodigoCliente").ToString(), "", "", "", "", "", "", "", "", "", "", "").Tables(0).Rows(0)("RazonSocial").ToString()
            Catch ex As Exception
                dts.Tables(0).Rows(i)("RazonSocial") = "Error, Cliente no existe"
            End Try
        Next
        Return dts
    End Function
    Public Function ModificarEstado(ByVal idRecibo As String, ByVal Estado As String) As Boolean
        Return Recibo.ModificarEstado(idRecibo, Estado)
    End Function
    Public Function EliminarImputaciones(ByVal idRecibo As String) As Boolean
        Dim Eliminado As Boolean = True
        Eliminado = Eliminado And impRecCaja.EliminarRecibo(idRecibo)
        Eliminado = Eliminado And impRecRet.EliminarRecibo(idRecibo)
        Eliminado = Eliminado And impRecCuenta.EliminarRecibo(idRecibo)
        Eliminado = Eliminado And impRecCheque.EliminarRecibo(idRecibo)

        'Eliminado = Eliminado And impRecCaja.Eliminar(impRecCaja.BuscarFiltrado("", "", idRecibo).Tables(0).Rows(0)("IdImpRecCaja").ToString())
        'Eliminado = Eliminado And impRecRet.Eliminar(impRecRet.BuscarFiltrado("", "", idRecibo).Tables(0).Rows(0)("IdImpRecRet").ToString())
        'Eliminado = Eliminado And impRecCuenta.Eliminar(impRecCuenta.BuscarFiltrado("", idRecibo, "").Tables(0).Rows(0)("IdImpRecCuenta").ToString())
        'Eliminado = Eliminado And impRecCheque.Eliminar(impRecCheque.BuscarFiltrado("", idRecibo, "").Tables(0).Rows(0)("IdImpRecCheque").ToString())
        Return Eliminado
    End Function

    Public Function GrabarImputaciones(ByVal DtsImputaciones As DataSet, ByVal idRecibo As String) As Boolean
        Dim okas As Boolean = True
        If Not (DtsImputaciones Is Nothing) Then
            For Each row As DataRow In DtsImputaciones.Tables(0).Rows
                If row("Nombre").ToString() = "Caja" Then
                    okas = okas And impRecCaja.Insertar(row("Total").ToString(), idRecibo)
                End If
                If row("Nombre").ToString() = "Retenciones" Then
                    Dim IdRetencion As String = Retencion.Insertar(row("DescripcionRetencion").ToString(), row("Total").ToString(), row("NumeroRetencion").ToString()).Tables(0).Rows(0)("IdRetencion").ToString()
                    okas = okas And impRecRet.Insertar(IdRetencion, idRecibo)
                End If
                If row("IdCheque").ToString() <> "" Then
                    okas = okas And impRecCheque.Insertar(idRecibo, row("IdCheque").ToString())
                End If
                If row("Cuenta").ToString() <> "" Then
                    okas = okas And impRecCuenta.Insertar(idRecibo, row("Cuenta").ToString(), row("Total").ToString())
                End If
            Next
        End If
        Return okas
    End Function

End Class
